import { defineStore } from 'pinia'
import http from '../common/request'
import { TERMINAL_TYPE } from '../common/enums.js'

export default defineStore('friendStore', {
  state: () => {
    return {
      friends: [], // 好友列表
      requests: [], // 好友申请列表
      timer: null,
    }
  },
  actions: {
    setFriends(friends) {
      friends.forEach((f) => {
        f.online = false
        f.onlineWeb = false
        f.onlineApp = false
      })
      this.friends = friends
      console.log(this.friends)
    },
    setRequests(requests) {
      this.requests = requests
    },
    addRequest(request) {
      this.requests.unshift(request)
    },
    removeRequest(id) {
      for (let idx in this.requests) {
        if (this.requests[idx].id == id) {
          this.requests.splice(idx, 1)
          break
        }
      }
    },
    updateFriend(friend) {
      let f = this.findFriend(friend.id)
      let copy = JSON.parse(JSON.stringify(f))
      Object.assign(f, friend)
      f.online = copy.online
      f.onlineWeb = copy.onlineWeb
      f.onlineApp = copy.onlineApp
    },
    removeFriend(id) {
      this.friends.filter((f) => f.id == id).forEach((f) => (f.deleted = true))
    },
    addFriend(friend) {
      if (this.friends.some((f) => f.id == friend.id)) {
        this.updateFriend(friend)
      } else {
        this.friends.unshift(friend)
      }
    },
    setOnlineStatus(onlineTerminals) {
      this.friends.forEach((f) => {
        let userTerminal = onlineTerminals.find((o) => f.id == o.userId)
        if (userTerminal) {
          f.online = true
          f.onlineWeb = userTerminal.terminals.indexOf(TERMINAL_TYPE.WEB) >= 0
          f.onlineApp = userTerminal.terminals.indexOf(TERMINAL_TYPE.APP) >= 0
        } else {
          f.online = false
          f.onlineWeb = false
          f.onlineApp = false
        }
      })
    },
    refreshOnlineStatus() {
      let userIds = this.friends.filter((f) => !f.deleted).map((f) => f.id)
      if (userIds.length == 0) {
        return
      }

      http({
        url: '/user/terminal/online?userIds=' + userIds.join(','),
        method: 'GET',
      }).then((onlineTerminals) => {
        this.setOnlineStatus(onlineTerminals)
      })

      // 30s后重新拉取
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.refreshOnlineStatus()
      }, 30000)
    },
    clear() {
      clearTimeout(this.timer)
      this.friends = []
      this.timer = null
    },
    loadFriend() {
      return new Promise((resolve, reject) => {
        http({
          url: '/friend/findFriendsForBlog',
          data: { blogUserId: uni.getStorageSync('userId') },
          method: 'POST',
        })
          .then((friends) => {
            console.log('friends', friends)
            this.setFriends(friends)
            this.refreshOnlineStatus()
            this.loadRequest()
            resolve()
          })
          .catch((res) => {
            reject()
          })
      })
    },
    loadRequest() {
      http({
        url: '/friend/request/list',
        method: 'GET',
      }).then((requests) => {
        this.setRequests(requests)
      })
    },
  },
  getters: {
    isFriend: (state) => (userId) => {
      return state.friends.filter((f) => !f.deleted).some((f) => f.id == userId)
    },
    findFriend: (state) => (id) => {
      return state.friends.find((f) => f.id == id)
    },
    findRequest: (state) => (id) => {
      return state.requests.find((req) => req.id == id)
    },
    isInRecvRequest: (state) => (userId) => {
      return state.requests.some((req) => req.recvId == userId)
    },
    isInSendRequest: (state) => (userId) => {
      return state.requests.some((req) => req.sendId == userId)
    },
  },
})
